<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="12">

          <a-col :md="4" :sm="8">
            <a-form-item label="操作类型:">
              <a-select v-model="queryParam.optionsType" placeholder="请选择用户状态查询">
                <a-select-option value>请选择操作类型</a-select-option>
                <a-select-option value="2">月卡延期</a-select-option>
                <a-select-option value="3">月卡注销</a-select-option>
                <a-select-option value="4">月卡开通</a-select-option>
                <a-select-option value="5">月卡修改</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :md="4" :sm="8">
            <a-form-item label="操作人">
              <a-input placeholder="请输入操作人名称" v-model="queryParam.createUserName"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="4" :sm="8">
            <a-form-item label="客户姓名">
              <a-input placeholder="请输入客户姓名" v-model="queryParam.userName"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="4" :sm="8">
            <a-form-item label="车牌号">
              <a-input placeholder="请输入车牌号" v-model="queryParam.plateNos"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="4" :sm="8">
            <a-form-item label="车位号">
              <a-input placeholder="请输入车位号" v-model="queryParam.parkNumbers"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="4" :sm="8">
            <a-form-item label="月租编号">
              <a-input placeholder="请输入月租编号" v-model="queryParam.monthlyCode"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="4" :sm="8">
            <a-form-item label="月租套餐">
              <a-select placeholder="请选择月租套餐" v-model="queryParam.packageId" :options="monthlyPackageInfoList">
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :md="4" :sm="8">
            <a-form-item label="操作时间">
              <j-date v-model="queryParam.opTime_begin" date-format="YYYY-MM-DD" placeholder="请选择开始时间"></j-date>
            </a-form-item>
          </a-col>
          <a-col :md="3" :sm="6">
            <a-form-item label="至">
              <j-date v-model="queryParam.opTime_end" date-format="YYYY-MM-DD" placeholder="请选择结束时间"></j-date>
            </a-form-item>
          </a-col>

          <a-col :md="4" :sm="8">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>

      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <a-button type="primary" icon="download" @click="handleExportXls('月卡操作记录表')">导出</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <a-menu slot="overlay">
            <a-menu-item key="1" @click="batchDel">
              <a-icon type="delete" />
              删除
            </a-menu-item>
          </a-menu>
          <a-button style="margin-left: 8px">
            批量操作
            <a-icon type="down" />
          </a-button>
        </a-dropdown>
      </div>

      <!-- table区域-begin -->

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">详情</a>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->
    <MonthlyCardEditInfoModal ref="detailModalForm"></MonthlyCardEditInfoModal>
    <!-- 表单区域 -->
  </a-card>
</template>

<script>
import MonthlyCardEditInfoModal from './modules/MonthlyCardEditInfoModalDetail'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { deleteAction, getAction, downFile } from '@/api/manage'
import { getMonthlyParkInfoList } from '@api/api'
import { filterObj } from '@/utils/util'
import ListParkNumberPopover from '@views/parking/parkmonthly/components/ListParkNumberPopover'

export default {
  name: 'MonthlyCardEditInfoList',
  mixins: [JeecgListMixin],
  components: {
    ListParkNumberPopover,
    MonthlyCardEditInfoModal,
  },
  data() {
    return {
      description: '1管理页面',
      // 表头
      columns: [
        {
          title: '停车场名称',
          align: 'center',
          dataIndex: 'parkName',
        },
        {
          title: '操作人名称',
          align: 'center',
          dataIndex: 'createUserName',
        },
        {
          title: '操作时间',
          align: 'center',
          dataIndex: 'updateTime',
        },
        {
          title: '操作类型',
          align: 'center',
          dataIndex: 'optionsType',
          customRender: function (t, r, index) {
            console.log('操作类型',t)
            if (t === 2 || t === '2') {
              return '月卡延期'
            }
            if (t === 3 || t === '3') {
              return '月卡注销'
            }
            if (t === 4 || t === '4') {
              return '月卡开通'
            }
            if (t === 5 || t === '5') {
              return '月卡修改'
            }
            if (t === 8 || t === '8') {
              return '月卡导入'
            }
            return ''
          },
        },
        {
          title: '订单金额(元)',
          align: 'center',
          dataIndex: 'payTheFee',
        },
        {
          title: '月租编号',
          align: 'center',
          dataIndex: 'monthlyCode',
        },
        {
          title: '套餐名称',
          align: 'center',
          dataIndex: 'packageName',
        },
        {
          title: '所属部门',
          align: 'center',
          dataIndex: 'monthlyDepartmentName',
        },
        {
          title: '车位号',
          align: 'center',
          dataIndex: 'parkNumbers',
          customRender: function (t, r, index) {
            console.log('车位信息',t)
            if (t === null || t === undefined || t === '') {
              return '无'
            }
            var array = t.split(',')
            if (array.length > 3) {
              let str = array[0] + ',' + array[1] + ',' + array[2]
              let moreInfo = ''
              for (let i = 0; i < array.length; i++) {
                moreInfo = moreInfo + array[i]
                if (i + 1 !== 0 && (i + 1) % 4 === 0) {
                  moreInfo = moreInfo + '\n'
                }
              }
              return (
                <list-park-number-popover
                  placement="bottom"
                  detail={array}
                  title="绑定车位信息"
                  simpleInfo={str}
                  trigger="click"
                  content="更多"
                />
              )
            }
            return t
          },
        },
        {
          title: '车牌信息',
          align: 'center',
          dataIndex: 'plateNos',
          customRender: function (t, r, index) {
            if (t === null || t === undefined || t === '') {
              return '无'
            }
            var array = t.split(',')
            if (array.length > 3) {
              let str = array[0] + ',' + array[1] + ',' + array[2]
              let moreInfo = ''
              for (let i = 0; i < array.length; i++) {
                moreInfo = moreInfo + array[i]
                if (i + 1 !== 0 && (i + 1) % 4 === 0) {
                  moreInfo = moreInfo + '\n'
                }
              }
              return (
                <list-park-number-popover
                  placement="bottom"
                  detail={array}
                  title="绑定车牌号信息"
                  simpleInfo={str}
                  trigger="click"
                  content="更多"
                />
              )
            }
            return t
          },
        },
        {
          title: '过期延期方式',
          align: 'center',
          dataIndex: 'overdueDelayWay',
          customRender: function (t, r, index) {
            if (t === 1 || t === '1') {
              return '顺延'
            }
            if (t === 2 || t === '2') {
              return '跳延'
            }
            return ''
          },
        },
        {
          title: '客户姓名',
          align: 'center',
          dataIndex: 'userName',
        },
        /*{
            title: '备注信息',
            align: "center",
            dataIndex: 'remark'
          },*/
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/com/monthlyCardEditInfo/list',
        delete: '/com/monthlyCardEditInfo/delete',
        deleteBatch: '/com/monthlyCardEditInfo/deleteBatch',
        exportXlsUrl: 'com/monthlyCardEditInfo/exportXls',
        getParkInfos: '/parking/parkInfo/parkInfoList',
        getMonthlyPackageList: '/monthlyPackageInfo/getMonthlyPackageInfoList',
      },
      queryParam: {
        optionsType: '',
        // "startTime": "",
        // "endTime": "",
        plateNo: '',
        createUserName: '',
        parkNumbers: '',
        parkCodes: '',
        userName: '',
        monthlyCode: '',
        packageId: undefined,
        type: '-1',
        opTime_begin: '',
        opTime_end: '',
      },
      isorter: {
        column: 'createTime',
        order: 'desc',
      },
      parkInfos: [{ label: '请选择车场', value: '' }],
      monthlyPackageInfoList: [{ label: '请选择月租套餐', value: '' }],
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  created() {
    this.keyupEnter()
    this.loadParkInfo()
    this.initMonthlyPackageInfoList()
  },
  methods: {
    initMonthlyPackageInfoList() {
      const that = this
      getAction(this.url.getMonthlyPackageList).then((res) => {
        if (res.success) {
          that.monthlyPackageInfoList = [{ label: '请选择月租套餐', value: '' }]
          if (res.result) {
            res.result.forEach((packageInfo) => {
              that.monthlyPackageInfoList.push({
                value: packageInfo.id,
                label: packageInfo.packageName,
              })
            })
          }
        }
      })
    },

    loadParkInfo() {
      const that = this
      //查看用户具有权限的车场
      getMonthlyParkInfoList().then((res) => {
        if (res.success) {
          if (res.result) {
            that.parkInfos = [{ label: '请选择车场', value: '' }]
            res.result.forEach((parkInfo) => {
              that.parkInfos.push({
                label: parkInfo.parkName,
                value: parkInfo.parkCode,
              })
            })
            // that.queryParam.parkCode = that.parkInfos[0].value;
          }
        }
      })
    },

    handleDetail: function (record) {
      this.$refs.detailModalForm.edit(record)
      this.$refs.detailModalForm.title = '详情'
      this.$refs.detailModalForm.disableSubmit = true
    },

    handleExportXls(fileName) {
      let param = { ...this.queryParam }
      downFile(this.url.exportXlsUrl, param).then((data) => {
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data]), fileName + '.xls')
        } else {
          let url = window.URL.createObjectURL(new Blob([data]))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
          window.URL.revokeObjectURL(url)
        }
      })
    },

    onChangeStartTime(date, dateString) {
      this.queryParam.opTime_begin = dateString
    },

    onChangeEndTime(date, dateString) {
      this.queryParam.opTime_end = dateString
    },

    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() //查询条件
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },

    searchQuery() {
      let opTime_begin = this.queryParam.opTime_begin
      let opTime_end = this.queryParam.opTime_end

      if (opTime_begin != '' && opTime_end != '') {
        if (opTime_begin > opTime_end) {
          this.$message.error('开始时间不能大于结束时间')
          return
        }
      }

      if (opTime_begin == '' && opTime_end != '') {
        this.$message.error('请选择开始时间')
        return
      }

      if (opTime_begin != '' && opTime_end == '') {
        this.$message.error('请选择结束时间')
        return
      }

      this.loadData(1)
    },

    getQueryParams() {
      var param = Object.assign({}, this.isorter, this.queryParam)
      param.field = this.getQueryField()
      param.pageNo = this.ipagination.current
      param.pageSize = this.ipagination.pageSize
      if (this.queryParam.opTime_begin != '' && this.queryParam.opTime_end != '') {
        param.opTime_begin = this.queryParam.opTime_begin + ' 00:00:00'
        param.opTime_end = this.queryParam.opTime_end + ' 23:59:59'
      }

      // delete param.createTimeRange; // 时间参数不传递后台
      return filterObj(param)
    },

    batchDel: function () {
      if (!this.url.deleteBatch) {
        this.$message.error('请设置url.deleteBatch属性!')
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = ''
        for (var a = 0; a < this.selectionRows.length; a++) {
          ids += this.selectionRows[a].invateuserdetailId + ','
        }
        var that = this
        this.$confirm({
          title: '确认删除',
          content: '是否删除选中数据?',
          onOk: function () {
            deleteAction(that.url.deleteBatch, { ids: ids }).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            })
          },
        })
      }
    },
    handleDelete: function (invateuserdetailId) {
      if (!this.url.delete) {
        this.$message.error('请设置url.delete属性!')
        return
      }
      var that = this
      deleteAction(that.url.delete, { invateuserdetailId: invateuserdetailId }).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },

    keyupEnter() {
      document.onkeydown = (e) => {
        let body = document.getElementsByTagName('body')[0]
        if (e.keyCode === 13) {
          this.loadData(1)
        }
      }
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>